package com.chart.web.controller.chart;
import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.RecognizeAllTextResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class OcrController {

    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    public  com.aliyun.ocr_api20210707.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(accessKeyId) // 替换成实际的AccessKeyId
                .setAccessKeySecret(accessKeySecret); // 替换成实际的AccessKeySecret
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        return new com.aliyun.ocr_api20210707.Client(config);
    }

    /**
     * 调用阿里云OCR接口，识别文本
     *
     * @param
     * @return 识别的文本
     */
    @GetMapping("/ocr")
    public String recognizeText(String imageurl) throws Exception {
        Client client = createClient();
        com.aliyun.ocr_api20210707.models.RecognizeAllTextRequest recognizeAllTextRequest = new com.aliyun.ocr_api20210707.models.RecognizeAllTextRequest()
                .setUrl(imageurl)
               // .setUrl("https://oss-hue.oss-cn-hangzhou.aliyuncs.com/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20241023120415.png")
                .setType("Advanced");  // 或者 "General"

        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        // 复制代码运行请自行打印 API 的返回值
        RecognizeAllTextResponse response = client.recognizeAllTextWithOptions(recognizeAllTextRequest, runtime);
         // 返回识别的文本内容
        return response.getBody().getData().getContent();
    }




}
